const config = require('./config');
const mosca = require('mosca');
const authorize = require('./authorize');

const server = new mosca.Server(config);
server.on('error', function (err) {
    console.log(err);
});

server.on('clientConnected', function (client) {
    console.log('client connected', client.id);
});

server.on('published', function (packet, client) {
    console.log('message:', packet.payload.toString())
})

server.on('clientDisconnected', function (client) {
    console.log('disconnected: ', client.id)
})

server.on('ready', setup);

function setup() {
    server.authenticate = authorize.authenticate;
    server.authorizePublish = authorize.authorizePublish;
    // server.authorizeSubscribe = authorize.authorizeSubscribe;
    console.log('Mosca server is up and running');
}
